package linker.plugin;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.jivesoftware.smack.Chat;

/**
 * 
 * 
 */
public class LinterRobotManager extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The Linter.
	 */
	private Linter linter;

	/**
	 * The list of robots.
	 */
	private JList robotList;

	/**
	 * The add button.
	 */
	private JButton addButton;

	/**
	 * The remove button.
	 */
	private JButton removeButton;

	/**
	 * The close button.
	 */
	private JButton closeButton;

	/**
	 * The buddyList.
	 */
	private JList buddyList;

	/**
	 * The model of buddyList.
	 */
	private DefaultListModel buddyListModel;

	/**
	 * The model of robots list.
	 */
	private DefaultListModel robotListModel;

	/**
	 * 
	 */
	public LinterRobotManager() {
		super("Linter-manage robots");
		setIconImage(this.getIconImage());
		setName("LinkerManageRobots");

		Container container = getContentPane();

		JPanel p = new JPanel();
		GroupLayout layout = new GroupLayout(p);
		p.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		// Initial buddyList.
		buddyList = new JList();
		buddyList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		buddyListModel = new DefaultListModel();
		buddyList.setModel(buddyListModel);
		JScrollPane buddyPane = new JScrollPane(buddyList);

		// Initial robots
		robotList = new JList();
		robotList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		robotListModel = new DefaultListModel();
		robotList.setModel(robotListModel);
		JScrollPane robotPane = new JScrollPane(robotList);

		addButton = new JButton("Add->");
		addButton.addActionListener(this);
		removeButton = new JButton("<-Remove");
		removeButton.addActionListener(this);
		closeButton = new JButton("Save&Close");
		closeButton.addActionListener(this);

		layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(buddyPane)).addGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(addButton).addComponent(removeButton)
						.addComponent(closeButton)).addGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(robotPane)));

		layout.setVerticalGroup(layout.createSequentialGroup().addGroup(
				layout.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addComponent(buddyPane).addGroup(
								layout.createSequentialGroup().addComponent(
										addButton).addComponent(removeButton)
										.addComponent(closeButton))
						.addComponent(robotPane)));

		container.add(p, BorderLayout.CENTER);

		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * Set linter.
	 * 
	 * @param paraLinter
	 *            The seted linter.
	 */
	public final void setLinter(final Linter paraLinter) {
		this.linter = paraLinter;

		ArrayList names = paraLinter.getNames();
		for (Object o : names) {
			buddyListModel.addElement(o);
		}

		names = paraLinter.getRobots();
		for (Object o : names) {
			Chat chat = (Chat) o;

			robotListModel.addElement(chat.getParticipant());
		}
	}

	/**
	 * The action performed.
	 * 
	 * @param e
	 *            The actionEvent.
	 */
	public final void actionPerformed(final ActionEvent e) {
		if (e.getSource() == closeButton) {
			linter.closeManager();
			linter.saveRobots();
			this.dispose();

		} else if (e.getSource() == addButton) {
			int i = buddyList.getSelectedIndex();
			if (i < 0) {
				return;
			}

			String name = (String) buddyListModel.getElementAt(i);
			for (Object o : robotListModel.toArray()) {
				String robot = (String) o;
				if (name.indexOf(robot) == 0) {
					return;
				}
			}

			robotListModel.addElement(name);
			linter.addRobot(name);
		} else if (e.getSource() == removeButton) {
			int i = robotList.getSelectedIndex();
			if (i < 0) {
				return;
			}

			linter.removeRobot((String) robotListModel.elementAt(i));
			robotListModel.removeElementAt(i);
		}
	}
}
